using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraPrinting.Native;
using QRCoder;
using RFS.Domain.PaiShi.Zuhuo;


namespace ProtoWinform
{
    public class ZuhuoPrintForm : Form
    {
        private List<ZuhuaRecord> _data;
        private PrintDocument _printDocument;


        private int w = 100.To100thInch(); //100 mm
        private int h = 60.To100thInch(); // 60 mm
        private int margin = 10.To100thInch();

        public PrintDocument PrintDocument
        {
            get { return _printDocument; }
        }


        public ZuhuoPrintForm(IEnumerable<ZuhuaRecord> data, int zhiPerza)
        {
            this.zhiPerza = zhiPerza;
            _data = data.ToList();

            

            // Create PrintDocument
            _printDocument = new PrintDocument();
            _printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", w, h); // 60mm x 100mm
            // _printDocument.PrintPage += PrintDocument_PrintPage;
            _printDocument.DefaultPageSettings.Margins =
                new Margins(margin, margin, margin, margin); // Set margins to 0 for better control

            // Get the current printer settings
            var ss = new PrinterSettings();
            ss.DefaultPageSettings.PaperSize = new PaperSize("Custom", w, h) ;
            _printDocument.PrinterSettings = ss;


            
            _printDocument.PrintPage += PrintDocument_PrintPage;
            _printDocument.EndPrint+= (sender, args) => _currentIndex=0 ;
        }

        private int _currentIndex = 0;
        public int zhiPerza = 20;

        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics graphics = e.Graphics;

            // Set the paper size to 100x60mm
            e.PageSettings.PaperSize = new PaperSize("Custom", w, h);
                
            // Get the current printer settings
            // PrinterSettings printerSettings = new PrinterSettings();
            // printerSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom", w, h)  ; 
            // PrintDocument.PrinterSettings = printerSettings; 


            // Set the margins (adjust as needed)
            e.PageSettings.Margins.Left = margin;
            e.PageSettings.Margins.Right = margin;
            e.PageSettings.Margins.Top = margin;
            e.PageSettings.Margins.Bottom = margin;

            // // Calculate margins (adjust as needed)
            int leftMargin = 10.To100thInch(); // 5mm in 1/100th of an inch
            int topMargin = 10.To100thInch(); // 5mm in 1/100th of an inch
            int lineHeight = 5.45.To100thInch(); // Adjust line height as needed

            // Font and pen settings
            Font drawFont = new Font("Arial", 10, FontStyle.Bold);
            Pen blackPen = new Pen(Color.Black);

            // Draw data for the current page
            if (_currentIndex < _data.Count)
            {
                var currentData = _data[_currentIndex];

                // Load and resize the image (if applicable)
                // Image image = Image.FromFile("te1.jpg"); // Replace with the actual image path
                Image image = qrCode(currentData.发货单码) ; // Replace with the actual image path
                image = ResizeImage(image,
                    new Size(60.To100thInch(), 60.To100thInch())); // Adjust width and height as needed
                // Draw the image   批次，供货代码，品种，等级，长度，数量，日期，复核人，录单人：9
                graphics.DrawImage(image, (35).To100thInch(), (-2).To100thInch());

                var txtTop = 20;
                var txtLeft = 20;
                var idx = 0;
       
                graphics.DrawString($"地块: {currentData.地块}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"批次: {currentData.批次}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                
                graphics.DrawString($"供货代码: {int.Parse(currentData.供货商码):000000}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"品种: {currentData.拍市品种名}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"等级: {currentData.等级}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"长度: {currentData.ProdDemo.长度} CM", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"数量: {currentData.枝数/zhiPerza:#} 扎", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"日期: {currentData.ZHTime:yyyy-M-d}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
 
                
                // extran
                graphics.DrawString($"复核人: {currentData.复核人}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
                graphics.DrawString($"录单人: {currentData.录单人}", drawFont, Brushes.Black, txtLeft, txtTop + idx++ *lineHeight   );
         
                
                graphics.DrawString($" {currentData.发货单码}", drawFont, Brushes.Black, txtLeft + 40.To100thInch() , txtTop + --idx *lineHeight + 1.To100thInch()   );
                
                _currentIndex++;
            }

            // Check if there are more pages to print
            e.HasMorePages = (_currentIndex < _data.Count);
        }

        Bitmap qrCode(string code)
        {
            // Generate QR Code
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode( code, QRCodeGenerator.ECCLevel.Q); // Adjust error correction level as needed
            QRCode qrCode = new QRCode(qrCodeData);
            Bitmap qrCodeImage = qrCode.GetGraphic(8); // Adjust scale as needed
            return qrCodeImage;
        }

        private Image ResizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

        public void Preview()
        {
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = _printDocument;
            // var rr = printPreviewDialog.ShowDialog();

             
            
            // if (rr == DialogResult.OK)
            // {
            //     // _printDocument.Print();
            // }
        }


        public static void PreviewForm(IEnumerable<ZuhuaRecord> o , int zhiPerZaKipa=20)
        {
            var f = new ZuhuoPrintForm(o,zhiPerZaKipa);
            // f.Preview();
            // f.ShowDialog();
        }
    }

    public static class Ex
    {
        public static int To100thInch(this double mm)
        {
            //1 cm = 0.393701 inches
            return (int)(mm * 1 / 25.4 * 100);
        }

        public static int To100thInch(this int mm)
        {
            return ((double)mm).To100thInch();
        }
    }
}